#pragma once

#include "OgreExMessageHandlerBase.h"
#include "OgreExIMessage.h"
#include "OgreExMessage.h"

namespace OgreEx
{
	template<class ActorType, class ValueType>
	class MessageHandler : public MessageHandlerBase
	{
	public:
		typedef void (ActorType::*HandlerFunction)(const ValueType &message);

		MessageHandler(ActorType *const actor, HandlerFunction function)
			: mActor(actor)
			, mHandlerFunction(function)
		{}
		virtual ~MessageHandler() {}

		
	public:
		inline virtual bool handle(IMessage* msg);

	protected:
		HandlerFunction mHandlerFunction;
		ActorType* mActor;
	};

	template<class ActorType, class ValueType>
	bool MessageHandler<ActorType,ValueType>::handle(IMessage* msg)
	{
		Message<ValueType> * factmsg = dynamic_cast<Message<ValueType>*>(msg);
		if( !factmsg  )
			return false;
		
		(mActor->*mHandlerFunction)(factmsg->value());
		factmsg->setHandled(true);
		return true;
	}
}